package com.bnzjy.srb.core.controller.api;


import com.bnzjy.common.exception.Assert;
import com.bnzjy.common.result.R;
import com.bnzjy.common.result.ResponseEnum;
import com.bnzjy.common.util.RegexValidateUtils;
import com.bnzjy.srb.base.util.JwtUtils;
import com.bnzjy.srb.core.pojo.vo.LoginVO;
import com.bnzjy.srb.core.pojo.vo.RegisterVO;
import com.bnzjy.srb.core.pojo.vo.UserIndexVO;
import com.bnzjy.srb.core.pojo.vo.UserInfoVO;
import com.bnzjy.srb.core.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 会员用户表
 */
@Api(tags = "会员接口")
@RestController
@RequestMapping("/api/core/userInfo")
@Slf4j
//@CrossOrigin
public class ApiUserInfoController {
    
    @Resource
    UserInfoService userInfoService;
    
    @Resource
    RedisTemplate redisTemplate;
    
    /**
     * 进行会员注册功能接口实现
     * @param registerVO
     * @return
     */
    @ApiOperation("会员注册")
    @PostMapping("/register")
    public R register(@Valid @RequestBody RegisterVO registerVO) {
        String mobile = registerVO.getMobile();
        String password = registerVO.getPassword();
        String code = registerVO.getCode();
    
        //MOBILE_NULL_ERROR(-202, "手机号不能为空"),
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        //MOBILE_ERROR(-203, "手机号不正确"),
        Assert.isTrue(RegexValidateUtils.checkCellphone(mobile), ResponseEnum.MOBILE_ERROR);
        //PASSWORD_NULL_ERROR(-204, "密码不能为空"),
        Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);
        // PASSWORD_TYPE_ERROR(-212, "密码格式错误"),
        Assert.isTrue(RegexValidateUtils.checkPassword(password), ResponseEnum.PASSWORD_TYPE_ERROR);
        System.out.println("密码为==>>" + password);
        //CODE_NULL_ERROR(-205, "验证码不能为空"),
        Assert.notEmpty(code, ResponseEnum.CODE_NULL_ERROR);
    
        //校验验证码
        String codeGen = (String) redisTemplate.opsForValue().get("srb:sms:srb:code:" + mobile);
        
        //CODE_ERROR(-206, "验证码不正确"),
        Assert.equals(code, codeGen, ResponseEnum.CODE_ERROR);
       
        //注册
        userInfoService.register(registerVO);
        return R.ok().message("注册成功");
    }
    
    /**
     * 进行登录接口实现
     * @param loginVO
     * @param request
     * @return
     */
    @ApiOperation("会员登录")
    @PostMapping("/login")
    public R login(@Valid @RequestBody LoginVO loginVO, HttpServletRequest request) {
      
        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);
        
        
        String ip = request.getRemoteAddr();
     
        UserInfoVO userInfoVO = userInfoService.login(loginVO, ip);
        return R.ok().data("userInfo", userInfoVO);
    }
    
    /**
     * 进行令牌校验，判断是否登录过
     * @param request
     * @return
     */
    @ApiOperation("校验令牌")
    @GetMapping("/checkToken")
    public R checkToken(HttpServletRequest request) {
        String token = request.getHeader("token");
       
        boolean result = JwtUtils.checkToken(token);
        if(result){
            return R.ok();
        }else{
            //LOGIN_AUTH_ERROR(-211, "未登录"),
            return R.setResult(ResponseEnum.LOGIN_AUTH_ERROR);
        }
    }
    
    /**
     * 校验手机号是否注册
     * @param mobile
     * @return
     */
    @ApiOperation("校验手机号是否被注册过")
    @GetMapping("/checkMobile/{mobile}")
    public boolean checkMobile(@PathVariable String mobile) {
        return userInfoService.checkMobile(mobile);
    }
    
    /**
     * 个人中心首页接口
     * @param request
     * @return
     */
    @ApiOperation("获取个人空间用户信息")
    @GetMapping("/auth/getIndexUserInfo")
    public R getIndexUserInfo(HttpServletRequest request) {
        
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        UserIndexVO userIndexVO = userInfoService.getIndexUserInfo(userId);
        
        return R.ok().data("userIndexVO", userIndexVO);
    }
}
